package com.flyan.game.motafx.effect.npc;

import com.almasb.fxgl.entity.Entity;
import com.flyan.game.motafx.component.LifePanelComponent;
import com.flyan.game.motafx.core.algorithm.AlterTransactionAlgorithm;
import com.flyan.game.motafx.core.util.console.ConsoleUtils;
import com.flyan.game.motafx.core.util.sound.SoundUtils;
import com.flyan.game.motafx.effect.BaseEffect;
import com.flyan.game.motafx.ui.TransactionPage;
import javafx.util.Duration;
import lombok.Data;
import org.jetbrains.annotations.NotNull;

/**
 * 祭坛交易完成效果
 *
 * @author flyan
 * @version 1.0
 * @email flyan_by_chenu@sina.com
 * @since 2025/2/10
 */
@Data
public class AlterTransactionFinishEffect extends BaseEffect {

    private static final AlterTransactionAlgorithm ALTER_TRANSACTION_ALGORITHM
            = new AlterTransactionAlgorithm(20);

    public AlterTransactionFinishEffect() {
        super(Duration.seconds(0.1));
    }

    @Override
    public void onStart(@NotNull Entity entity) {
        TransactionPage.Item item = extInfo.get("item", TransactionPage.Item.class);
        LifePanelComponent lifePanelComponent = entity.getComponent(LifePanelComponent.class);

        /* 此次交易金额是否足够？ */
        int transactionAmount = ALTER_TRANSACTION_ALGORITHM.transactionAmount();
        if (lifePanelComponent.getLifePanel().getGold() < transactionAmount) {
            ConsoleUtils.println("你的金币不足%d枚，无法供奉！".formatted(transactionAmount));
            return;
        }

        SoundUtils.play("item.wav");
        ALTER_TRANSACTION_ALGORITHM.completeTransaction(); // 交易完成
        switch (item.getId()) { // 赋予能力
            case 0 -> lifePanelComponent.getLifePanel().increaseHp(item.getValue());
            case 1 -> lifePanelComponent.getLifePanel().increaseAck(item.getValue());
            case 2 -> lifePanelComponent.getLifePanel().increaseDef(item.getValue());
        }
        lifePanelComponent.getLifePanel().increaseGold(-transactionAmount);
        lifePanelComponent.update();
    }

    @Override
    public void onEnd(@NotNull Entity entity) {

    }

}
